[−][src]Crate nats
A Rust client for the NATS.io ecosystem.
git clone https://github.com/nats-io/nats.rs
NATS.io is a simple, secure and high performance open source messaging system for cloud native
applications, IoT
messaging, and microservices architectures.
For more information see https://nats.io/.
Examples
> cargo run --example nats-box -- -h
Basic connections, and those with options. The compiler will force these to be correct.
let nc = nats::connect("demo.nats.io")?; let nc2 = nats::Options::with_user_pass("derek", "s3cr3t!") .with_name("My Rust NATS App") .connect("127.0.0.1")?; let nc3 = nats::Options::with_credentials("path/to/my.creds") .connect("connect.ngs.global")?; let tls_connector = nats::tls::builder() .identity(nats::tls::Identity::from_pkcs12(b"der_bytes", "my_password")?) .add_root_certificate(nats::tls::Certificate::from_pem(b"my_pem_bytes")?) .build()?; let nc4 = nats::Options::new() .tls_connector(tls_connector) .connect("tls://demo.nats.io:4443")?;
Publish
let nc = nats::connect("demo.nats.io")?; nc.publish("my.subject", "Hello World!")?; nc.publish("my.subject", "my message")?; // Publish a request manually. let reply = nc.new_inbox(); let rsub = nc.subscribe(&reply)?; nc.publish_request("my.subject", &reply, "Help me!")?;
Subscribe
let nc = nats::connect("demo.nats.io")?; let sub = nc.subscribe("foo")?; for msg in sub.messages() {} // Using next. if let Some(msg) = sub.next() {} // Other iterators. for msg in sub.try_iter() {} for msg in sub.timeout_iter(Duration::from_secs(10)) {} // Using a threaded handler. let sub = nc.subscribe("bar")?.with_handler(move |msg| { println!("Received {}", &msg); Ok(()) }); // Queue subscription. let qsub = nc.queue_subscribe("foo", "my_group")?;
Request/Response
let nc = nats::connect("demo.nats.io")?; let resp = nc.request("foo", "Help me?")?; // With a timeout. let resp = nc.request_timeout("foo", "Help me?", Duration::from_secs(2))?; // With multiple responses. for msg in nc.request_multi("foo", "Help")?.iter() {} // Publish a request manually. let reply = nc.new_inbox(); let rsub = nc.subscribe(&reply)?; nc.publish_request("foo", &reply, "Help me!")?; let response = rsub.iter().take(1);
Re-exports
pub use subscription::Subscription; |
Modules
subscription | Functionality relating to subscribing to a subject. |
tls | Functionality relating to TLS configuration |
Structs
Connection | A NATS connection. |
Message | A |
Options | Connect options. |
ServerInfo | Information sent by the server back to this client during initial connection, and possibly again later. |
Functions
connect | Connect to a NATS server at the given url. |